#include <iostream>
#include <vector>
#include <pthread.h>
int g_value = 100;
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *ticket_system(void *args)
{
    // std::string name = (char*)args;
    while (1)
    {
        pthread_mutex_lock(&mutex);
        if (g_value > 0)
        {
            --g_value;
            printf("thread get ticket: %d \n", g_value);
        }
        pthread_mutex_unlock(&mutex);
    }
}
int main()
{

    pthread_t thread_id;
    int result;
    std::vector<pthread_t> ids;
    // 创建线程
    for (int i = 0; i < 5; i++)
    {
        pthread_t thread_id;
        std::string name = "thread-"+ std::to_string(i);
        result = pthread_create(&thread_id, NULL, ticket_system, (void*)&name);
        ids.push_back(thread_id);
    }

    for (auto id : ids)
    {
        pthread_join(id, NULL);
    }
    return 0;
}